package jin.chen.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import jin.chen.pojo.Carousel;
import jin.chen.pojo.Category;
import jin.chen.pojo.vo.CategoryVO;
import jin.chen.pojo.vo.SixNewItemVO;
import jin.chen.service.CarouselService;
import jin.chen.service.CategoryService;
import jin.chen.utils.Result;
import jin.chen.utils.enums.CarouselShowEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * 首页相关的controller
 */
@Api(value = "首页", tags = "首页展示controller")
@RestController
@RequestMapping("/index")
public class IndexController {
    @Autowired
    private CarouselService carouselService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询数据库中轮播图的相关信息，
     * 方法中参数为轮播图的一个是否显示字段
     * @return
     */
    @ApiOperation(value = "获取首页轮播图列表接口", notes = "获取首页轮播图列表接口", httpMethod = "GET")
    @GetMapping("/carousel")
   public Result Carousel(){
      List<Carousel> list =  carouselService.queryAllCarousel(CarouselShowEnum.yes.getType());
      return Result.OK(list);
    }

    /**
     * 查询首页上的所有一级分类信息
     * @return
     */
    @ApiOperation(value = "获取商品一级分类列表接口", notes = "获取首页一级分类列表接口", httpMethod = "GET")
    @GetMapping("/cats")
    public Result Category(){
        List<Category> list =  categoryService.queryAllRootType();
        return Result.OK(list);
    }

    /**
     * 根据一级分类id查询子分类信息
     * @param rootId
     * @return
     */
    @ApiOperation(value = "获取商品子分类列表接口", notes = "获取商品子分类列表接口", httpMethod = "GET")
    @GetMapping("/subCat/{rootId}")
    public Result subCat(@ApiParam(name = "rootId", value = "一级分类id", required = true)
                             @PathVariable Integer rootId){
        if (rootId == null){
            return Result.errorMsg("分类不存在");
        }
        List<CategoryVO> list =  categoryService.querySubCatType(rootId);
        return Result.OK(list);
    }

    /**
     * 根据一级分类id查询下面最新的六条商品数据信息
     * @param rootId
     * @return
     */
    @ApiOperation(value = "获取一级分类下最新六条商品数据信息接口", notes = "获取一级分类下最新六条商品数据信息接口", httpMethod = "GET")
    @GetMapping("/sixNewItems/{rootId}")
    public Result sixNewItems(@ApiParam(name = "rootId", value = "一级分类id", required = true)
                         @PathVariable Integer rootId){
        if (rootId == null){
            return Result.errorMsg("分类不存在");
        }
        List<SixNewItemVO> list =  categoryService.querySixNewItemLazy(rootId);
        return Result.OK(list);
    }
}
